﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;


public partial class ManterGenero : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    
    protected void btNovo_Click(object sender, EventArgs e)
    {
        btGravar.Enabled = true;
        btCancelar.Enabled = true;
        tdNome.Enabled = true;
        ddTipo.Enabled = true;
        btNovo.Enabled = false;
    }
   
    protected void btCancelar_Click(object sender, EventArgs e)
    {
        resetarTela();
    }
    
    protected void btGravar_Click(object sender, EventArgs e)
    {
        try
        {
            GeneroBLL generoBLL = new GeneroBLL();
            if (tdCodigo.Text == "")
            {
                generoBLL.Inserir(new Genero(tdNome.Text, Convert.ToInt32(ddTipo.SelectedValue)));
            }
            else
            {
                generoBLL.Atualizar(new Genero(Convert.ToInt32(tdCodigo.Text), tdNome.Text, Convert.ToInt32(ddTipo.SelectedValue)));
            }
            resetarTela();
            GridView1.DataBind();
        }
        catch(Exception ex)
        {
            lbErro.Text = ex.Message;
        }
    }

    protected void lbRemove_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
            new GeneroBLL().Remover(new Genero(id));
            GridView1.DataBind();
        }
        catch(Exception ex) 
        {
            lbErro.Text = ex.Message;
        }
        
    }
   
    protected void lbAtualiza_Click(object sender, EventArgs e)
    {
        btGravar.Enabled = true;
        btCancelar.Enabled = true;
        tdNome.Enabled = true;
        ddTipo.Enabled = true;
        btNovo.Enabled = false;

        int id = Convert.ToInt32(((GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent).Cells[0].Text);
        Genero genero = (new GeneroBLL().Consultar(id));
        if (genero == null)
        {
            lbErro.Text = "Não foi possível obter os dados no banco";
            return;
        }
        tdCodigo.Text = genero.Codigo.ToString();
        tdNome.Text = genero.Nome;
        ddTipo.SelectedValue = genero.Tipo.ToString();
    }

    private void resetarTela()
    {
        GridView1.DataBind();
        tdCodigo.Text = "";
        tdNome.Text = "";
        ddTipo.SelectedValue = "0";

        btGravar.Enabled = false;
        btCancelar.Enabled = false;
        tdNome.Enabled = false;
        ddTipo.Enabled = false;
        btNovo.Enabled = true;
        lbErro.Text = "";
    }
}
